/**
 * ScriptDev3 is an extension for mangos providing enhanced features for
 * area triggers, creatures, game objects, instances, items, and spells beyond
 * the default database scripting in mangos.
 *
 * Copyright (C) 2006-2013  ScriptDev2 <http://www.scriptdev2.com/>
 * Copyright (C) 2014-2019  MaNGOS  <https://getmangos.eu>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * World of Warcraft, and all World of Warcraft or Warcraft art, images,
 * and lore are copyrighted by Blizzard Entertainment, Inc.
 */

/**
 * ScriptData
 * SDName:      Boss_Broodlord_Lashlayer
 * SD%Complete: 100
 * SDComment:   None
 * SDCategory:  Blackwing Lair
 * EndScriptData
 */

#include "precompiled.h"
#include "blackwing_lair.h"

enum
{
    SAY_AGGRO                   = -1469000,
    SAY_LEASH                   = -1469001,

    SPELL_CLEAVE                = 26350,
    SPELL_BLAST_WAVE            = 23331,
    SPELL_MORTAL_STRIKE         = 24573,
    SPELL_KNOCK_AWAY            = 25778
};

struct boss_broodlord : public CreatureScript
{
    boss_broodlord() : CreatureScript("boss_broodlord") {}

    struct boss_broodlordAI : public ScriptedAI
    {
        boss_broodlordAI(Creature* pCreature) : ScriptedAI(pCreature)
        {
            m_pInstance = (ScriptedInstance*)pCreature->GetInstanceData();
        }

        ScriptedInstance* m_pInstance;

        uint32 m_uiCleaveTimer;
        uint32 m_uiBlastWaveTimer;
        uint32 m_uiMortalStrikeTimer;
        uint32 m_uiKnockAwayTimer;

        void Reset() override
        {
            m_uiCleaveTimer = 8000;                     // These times are probably wrong
            m_uiBlastWaveTimer = 12000;
            m_uiMortalStrikeTimer = 20000;
            m_uiKnockAwayTimer = 30000;
        }

        void Aggro(Unit* /*pWho*/) override
        {
            if (m_pInstance)
            {
                m_pInstance->SetData(TYPE_LASHLAYER, IN_PROGRESS);
            }

            DoScriptText(SAY_AGGRO, m_creature);
        }

        void JustDied(Unit* /*pKiller*/) override
        {
            if (m_pInstance)
            {
                m_pInstance->SetData(TYPE_LASHLAYER, DONE);
            }
        }

        void JustReachedHome() override
        {
            if (m_pInstance)
            {
                m_pInstance->SetData(TYPE_LASHLAYER, FAIL);
            }
        }

        void UpdateAI(const uint32 uiDiff) override
        {
            if (!m_creature->SelectHostileTarget() || !m_creature->getVictim())
            {
                return;
            }

            // Cleave Timer
            if (m_uiCleaveTimer < uiDiff)
            {
                if (DoCastSpellIfCan(m_creature->getVictim(), SPELL_CLEAVE) == CAST_OK)
                {
                    m_uiCleaveTimer = 7000;
                }
            }
            else
            {
                m_uiCleaveTimer -= uiDiff;
            }

            // Blast Wave
            if (m_uiBlastWaveTimer < uiDiff)
            {
                if (DoCastSpellIfCan(m_creature, SPELL_BLAST_WAVE) == CAST_OK)
                {
                    m_uiBlastWaveTimer = urand(8000, 16000);
                }
            }
            else
            {
                m_uiBlastWaveTimer -= uiDiff;
            }

            // Mortal Strike Timer
            if (m_uiMortalStrikeTimer < uiDiff)
            {
                if (DoCastSpellIfCan(m_creature->getVictim(), SPELL_MORTAL_STRIKE) == CAST_OK)
                {
                    m_uiMortalStrikeTimer = urand(25000, 35000);
                }
            }
            else
            {
                m_uiMortalStrikeTimer -= uiDiff;
            }

            if (m_uiKnockAwayTimer < uiDiff)
            {
                DoCastSpellIfCan(m_creature->getVictim(), SPELL_KNOCK_AWAY);
                // Drop 50% aggro - TODO should be scriptedEffect?
                if (m_creature->GetThreatManager().getThreat(m_creature->getVictim()))
                {
                    m_creature->GetThreatManager().modifyThreatPercent(m_creature->getVictim(), -50);
                }

                m_uiKnockAwayTimer = urand(15000, 30000);
            }
            else
            {
                m_uiKnockAwayTimer -= uiDiff;
            }

            DoMeleeAttackIfReady();

            if (EnterEvadeIfOutOfCombatArea(uiDiff))
            {
                DoScriptText(SAY_LEASH, m_creature);
            }
        }
    };
    CreatureAI* GetAI(Creature* pCreature) override
    {
        return new boss_broodlordAI(pCreature);
    }
};

void AddSC_boss_broodlord()
{
    Script* s;
    s = new boss_broodlord();
    s->RegisterSelf();

    //pNewScript = new Script;
    //pNewScript->Name = "boss_broodlord";
    //pNewScript->GetAI = &GetAI_boss_broodlord;
    //pNewScript->RegisterSelf();
}
